// pages/shoppingCart/shoppingCart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    authorization: true,//授权隐藏
    emptyCart: true,//购物车为空隐藏

    carts: [
      {
        onceprice: '11', name: 'USA', value: '美国', goodsTitle: '赣南脐橙500g 品质', price: '11', minusStatus: "disable", num: '1', selected: false, checked:''},
      { onceprice: '12', name: 'CHN', value: '中国', goodsTitle: '赣南脐橙500g 品质', price: '12', minusStatus: "disable", num: '1', selected: false, checked: '' },
      { onceprice: '13', name: 'BRA', value: '巴西', goodsTitle: '赣南脐橙500g 品质', price: '13', minusStatus: "disable", num: '1', selected: false, checked: ''},
      { onceprice: '14', name: 'JPN', value: '日本', goodsTitle: '赣南脐橙500g 品质', price: '14', minusStatus: "disable", num: '1', selected: false, checked: ''},
      { onceprice: '15', name: 'ENG', value: '英国', goodsTitle: '赣南脐橙500g 品质', price: '15', minusStatus: "disable", num: '1', selected: false, checked: ''},
      { onceprice: '16', name: 'TUR', value: '法国', goodsTitle: '赣南脐橙500g 品质', price: '16', minusStatus: "disable", num: '1', selected: false, checked: ''},
    ],

    selectedAllStatus: false,
    allprice:'0',

  },
  
  /*点击减号*/
  bindMinus: function (e) {
    var that = this;
    var arr_key = e.currentTarget.dataset.key;
    var carts = this.data.carts;
    var num = carts[arr_key].num;
    if (num > 1) {
      num--;
    };
    carts[arr_key].num = num;
    carts[arr_key].minusStatus = carts[arr_key].num > 1 ? 'normal' : 'disable';
    var theminusStatus = carts[arr_key].minusStatus;
    var allcost = 0;
    var theallprice = 0;
    for (var i = 0; i < carts.length; i++) {
      var inum = parseFloat(that.data.carts[i].num);
      var iprice = parseFloat(that.data.carts[i].price);
      var selected = carts[i].selected;
      if (!selected) {
      } else {
        allcost = inum * iprice;
        theallprice += allcost;//累加
      }
      that.setData({
        allprice: theallprice,
        carts: carts,
      });
    }
  },
  /*点击加号*/
  bindPlus: function (e) {
    var that = this;
    var arr_key = e.currentTarget.dataset.key;
    var carts = that.data.carts;
    var num = carts[arr_key].num;
    num++;
    carts[arr_key].num = num;
    carts[arr_key].minusStatus = carts[arr_key].num > 1 ? 'normal' : 'disable';
    var theminusStatus = carts[arr_key].minusStatus;
   
    var allcost = 0;
    var theallprice = 0;  
    for (var i = 0; i < carts.length; i++) {
      var inum = parseFloat(that.data.carts[i].num);
      var iprice = parseFloat(that.data.carts[i].price);
      var selected = carts[i].selected;
      if (!selected) {
      } else {
        allcost = inum * iprice;
        theallprice += allcost;//累加
      }
      that.setData({
        allprice: theallprice,
        carts: carts,
      });
    }
  },
  //点击删除
  toclear:function(){
    var that = this;
  },

  // // //单个选择
  bindCheckbox: function (e) {
    var that = this;
    var allcost = 0;
    var theallprice = 0;
    var goodsNumBox = [];
    var index = parseFloat(e.currentTarget.dataset.index);
    var selected = that.data.carts[index].selected;
    var carts = that.data.carts;
    carts[index].selected = !selected;
    for (var i = 0; i < carts.length; i++) {
      var num = parseFloat(that.data.carts[i].num);
      var price = parseFloat(that.data.carts[i].price);
      var selected = carts[i].selected;
      // console.log(num, price)
      if (!selected) {
        // console.log("!selected")
      } else {
        goodsNumBox.push("1")
        // console.log("selected")
        allcost = num * price;
        // console.log(allcost);
        theallprice += allcost;//累加
      }
      // console.log(goodsNumBox)
      that.setData({
        allprice: theallprice,
        carts: carts,
      });
    }
    if (goodsNumBox.length == carts.length){
      // console.log("全选")
      that.setData({
        selectedAllStatus: true,
      }); 
    }
  },

  //全选
  bindSelectAll: function (e) {
    var that = this;    
    that.setData({
      selectedAllStatus : true,
    });    
    var selectedAllStatus = that.data.selectedAllStatus;
    selectedAllStatus = !selectedAllStatus;
    var carts = that.data.carts;
    var allcost = 0; //某个的总价
    var theallprice = 0; //总数总价格
    if (!selectedAllStatus) {
      for (var i = 0; i < carts.length; i++) {
        carts[i].selected = true
        var num = parseFloat(that.data.carts[i].num);
        var price = parseFloat(that.data.carts[i].price);
        // console.log(num, price);
        allcost = num * price;
        // console.log(allcost);
        theallprice += allcost ;//累加
      }      
      // console.log("总数", theallprice)
      that.setData({
        allprice: theallprice,
        carts: carts,
      });
    }
  },

  //去结算
  settlement:function(){
    // console.log(this)
    // console.log(this.data.allprice)
    var allprice = this.data.allprice;
    if (allprice == 0){
      wx.showToast({
        title: '请选择结算商品',
        icon: 'none',
        duration: 2000
      })
    }else{
      wx.navigateTo({
        url: '/pages/submitOrders/submitOrders'
      })
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})